﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Collections.Generic;

public partial class CustomerManagement_MoneyManagement : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDropCreaterAndChecker();
            AspNetPager1.PageSize = 50;
            BindData();
        }
        Session["FCode"] = "300400112";

    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (!Check.CheckPermission("300400112", "00001"))
        {
            Share.Alert(this.Page, "无权操作"); return;
        }
        Response.Redirect("MoneyManagementEdit.aspx");
    }
    private void BindData()
    {
        try
        {
            StringBuilder Sql = new StringBuilder();
            Sql.Append(" where 1=1");
            Sql.Append(this.txtBillId.Text.Trim() == "" ? "" : " and R_ID='"+this.txtBillId.Text.Trim()+"'");
            //Sql.Append(this.dropCreater.SelectedValue == "-1" ? "" : " and R_MAKER='" + this.dropCreater.SelectedValue + "'");
            Sql.Append(this.dropAudit.SelectedValue == "-1" ? "" : " and R_VALID='" + this.dropAudit.SelectedValue + "'");
            Sql.Append(this.dropChecker.SelectedValue == "-1" ? "" : " and R_VALIDOR='" + this.dropChecker.SelectedValue + "'");
            Sql.Append(this.txtCustomerId.Text.Trim() == "" ? "" : " and R_USERCODE like'%" + this.txtCustomerId.Text.Trim() + "%'");
            Sql.Append(this.txtCreateDateStart.Text.Trim() == "" ? "" : " and R_ADDTIME>='" + this.txtCreateDateStart.Text.Trim() + "'");
            Sql.Append(this.txtCreateDateEnd.Text.Trim() == "" ? "" : " and R_ADDTIME<='" + this.txtCreateDateEnd.Text.Trim() + " 23:59:59.999'");
            Sql.Append(this.txtCheckDateStart.Text.Trim() == "" ? "" : " and R_VALIDTIME>='" + this.txtCheckDateStart.Text.Trim() + "'");
            Sql.Append(this.txtCheckDateEnd.Text.Trim() == "" ? "" : " and R_VALIDTIME<='" + this.txtCheckDateEnd.Text.Trim() + " 23:59:59.999'");
            int rowCount;
            DataSet ds = new BLL.REMITTANCE().GetRemittance_List(GetStartIndex(AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize), AspNetPager1.PageSize, Sql.ToString(), out rowCount);
            REMITTANCEListControl1.Bind(ds);
            AspNetPager1.RecordCount = rowCount;
        }
        catch(Exception ex)
        {
            Share.Alert(this.Page, ex.Message); return;
        }
    }
    private int GetStartIndex(int pageIndex, int pageSize)
    {
        return 1 + (pageIndex - 1) * pageSize;
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindData();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindData();
    }
#region 绑定数据到制单人和审核人下拉列表框
    private void BindDropCreaterAndChecker()
    {
        List<Model.USERS> users = new BLL.USERS().GetAllList();
        if (users != null)
        {
            dropChecker.DataSource = users;
            dropChecker.DataTextField = "USERS_NAME";
            dropChecker.DataValueField = "USERS_CODE";
            dropChecker.DataBind();
            /*
            dropCreater.DataSource = users;
            dropCreater.DataTextField = "USERS_NAME";
            dropCreater.DataValueField = "USERS_CODE";
            dropCreater.DataBind();
            */
            ListItem item = new ListItem();
            item.Text = "全部";
            item.Value = "-1";

            dropChecker.Items.Insert(0, item);
            //dropCreater.Items.Insert(0, item);
        }
    }
#endregion
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (Check.CheckPermission("300400112", "00003"))
        {
            List<int> s = REMITTANCEListControl1.Selected;
            bool flag = false;
            Model.REMITTANCE remittance=null;
            foreach (int id in s)
            {
                remittance = new BLL.REMITTANCE().GetModel(id);
                if (remittance.R_Valid == 1)
                {
                    flag = true;
                    break;
                }
            }
            if (flag)
            {
                Share.Alert(this, "已经审核的充值单不能删除"); return;
            }
            new BLL.REMITTANCE().Delete(s);
            Share.InsertLog("300400112", "00003");
            Response.Redirect("MoneyManagement.aspx");
        }
        else
        {
            Share.Alert(this.Page, "无权操作"); return;
        }
    }
}
